﻿using System;
using System.Collections.Generic;
using System.Drawing;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace breakoutsharp.visual.menu
{
    public class DxStartMenu : IDisposable
    {
        public enum MenuItems : byte
        {
            EXIT,
            OPTIONS,
            START
        }
        protected List<DxButton> buttons = new List<DxButton>(5);

        protected DxSprite backgroundSprite;
        protected Sprite sprite;
        protected DxSprite animationSprite;
        protected Microsoft.DirectX.Direct3D.Font menuFont;

        protected SizeF aniSize = new SizeF(4096, 128);
        protected int slides = 21;
        protected int slideNumber = 0;
        protected int slideWidth = 181;

        protected bool animationStarted = false;

        protected Point menuLocation;

        public DxStartMenu(Device dev, Point location)
        {
            backgroundSprite = new DxSprite(dev, "gfx", "menu", new Size(800, 600));
            sprite = new Sprite(dev);
            animationSprite = new DxSprite(dev, "gfx", "panel3", new Size(4096, 128), Color.FromArgb(255, 255, 0, 255));

            menuFont = new Microsoft.DirectX.Direct3D.Font(dev, new System.Drawing.Font("Arial", 14, FontStyle.Regular));
            menuLocation = location;

            DxButton startButton = new DxButton(dev, sprite, menuFont)
            {
                Text = "Start Game",
                TextColor = Color.Orange,
                Selected = true,
                Location = new Point(location.X + 15, location.Y + 10)
            };
            DxButton optionBtn = new DxButton(dev, sprite, menuFont)
            {
                Text = "Options",
                TextColor = Color.Orange,
                Selected = false,
                Location = new Point(location.X + 15, location.Y + 10 + 45),
            };
            DxButton exitBtn = new DxButton(dev, sprite, menuFont)
            {
                Text = "Exit Game",
                TextColor = Color.Orange,
                Selected = false,
                Location = new Point(location.X + 15, location.Y + 10 + 45 * 2)
            };

            buttons.Add(startButton);
            buttons.Add(optionBtn);
            buttons.Add(exitBtn);

        }

        public void Draw()
        {
            if (slideNumber < slides && animationStarted)
            {
                slideNumber += 1;
            }
            backgroundSprite.Draw(sprite, new Rectangle(), new Point(), new SizeF(1200, 900));
            animationSprite.Draw(sprite, new Rectangle(1 + 1 * slideNumber + slideWidth * slideNumber, 0, slideWidth, 128), menuLocation, new SizeF(slideWidth, 170));


            if (slideNumber == slides)
            {
                foreach (DxButton b in buttons)
                {
                    b.Draw();
                }
            }
        }

        public void OpenMenu()
        {
            slideNumber = 0;
            animationStarted = true;
        }

        protected bool _downFlag = false;
        protected bool _upFlag = false;

        public void ControlPressed(bool down, bool up)
        {
            if (down & !_downFlag)
            {
                NextButton();
                _downFlag = true;
            }
            else
            {
                _downFlag = down;
            }

            if (up & !_upFlag)
            {
                PrevButton();
                _upFlag = true;
            }
            else
            {
                _upFlag = up;
            }
        }

        public void NextButton()
        {
            for (int i = 0; i < buttons.Count; i++)
            {
                if (buttons[i].Selected)
                {
                    if (i != buttons.Count - 1)
                    {
                        buttons[i + 1].Selected = true;
                        buttons[i].Selected = false;
                    }
                    else
                    {
                        buttons[0].Selected = true;
                        buttons[i].Selected = false;
                    }
                    return;
                }
            }
        }


        public void PrevButton()
        {
            for (int i = 0; i < buttons.Count; i++)
            {
                if (buttons[i].Selected)
                {
                    if (i != 0)
                    {
                        buttons[i -1].Selected = true;
                        buttons[i].Selected = false;
                    }
                    else
                    {
                        buttons[0].Selected = false;
                        buttons[buttons.Count - 1].Selected = true;
                    }
                    return;
                }
            }
        }


        public DxButton SelectedItem
        {
            get
            {
                foreach (DxButton b in buttons)
                {
                    if (b.Selected)
                        return b;
                }

                return null;
            }
            set
            {
                if (value == null)
                    return;
                else
                {
                    foreach (DxButton b in buttons)
                    {
                        b.Selected = false;
                    }

                    value.Selected = true;
                }
            }
        }


        private bool disposedValue = false;
        // To detect redundant calls

        // IDisposable
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposedValue)
            {
                if (disposing)
                {
                    // TODO: free other state (managed objects).

                    if ((animationSprite != null))
                    {
                        animationSprite.Dispose();
                    }

                    if ((backgroundSprite != null))
                    {
                        backgroundSprite.Dispose();
                    }

                    foreach (DxButton b in buttons)
                    {
                        if (b != null)
                            b.Dispose();
                    }
                }
            }

            // TODO: free your own state (unmanaged objects).
            // TODO: set large fields to null.
            this.disposedValue = true;
        }

        #region " IDisposable Support "
        // This code added by Visual Basic to correctly implement the disposable pattern.
        public void Dispose()
        {
            // Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        #endregion

    }
}
